import dataset.*; 
import neuralnetwork.*;

import java.io.PrintStream;
import java.util.Random;


/**
 * 
 */

/**
 * @author decomite
 *
 */
public class TestNN {
	private static PrintStream output; 
	private static Random generator=new Random(); 
	private static double function(double x,double y){
		return (Math.sin(6.28*x)+Math.sin(6.28*y))/2; 
	}

	/**
	 * Un premier test d'utilisation : z=f(x,y)
	 * 
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
		
		double inputs[]=new double[2]; 
		double outputs[]=new double[1];
		int layersdescript[]={2,3,1}; 
		NeuralNetwork nn=new NeuralNetwork(layersdescript);
		nn.initNetwork(); 
		nn.setEpoch(100);
		Dataset ds=new Dataset();
		output=new PrintStream("/tmp/nn.txt");
		
		for(int i=0;i<10000;i++){
			inputs[0]=generator.nextDouble();
			inputs[1]=generator.nextDouble();
			outputs[0]=function(inputs[0],inputs[1]);
			ds.add(new Sample(inputs,outputs)); 	
		}
		
		double memoire[]=new double[100];
		double total=0; 
		
		output=new PrintStream("/tmp/nn.txt");
		
		for(int i=0;i<1000;i++){
		    nn.learnFromDatasetStochastic(ds); 
		    total=total-memoire[i%100]; 
		    memoire[i%100]=nn.getSE(); 
		    total=total+memoire[i%100];
		    double facteur; 
		    if(i<100) facteur=i+1; else facteur=100; 
		//    output.println(i+" "+nn.getSE()+" "+total/facteur);
		    System.out.println(i+" "+nn.getSE()+" "+total/facteur);
		}
		
		double xi=0; 
		double xpas=0.01;
		double ypas=0.01; 
		for(int i=0;i<100;i++){
			double yi=0; 
			inputs[0]=xi; 
			for(int j=0;j<100;j++){
				output.println(xi+" "+yi+" "+function(xi, yi)+" "+nn.classify(inputs)[0]);
				inputs[1]=yi; 
				yi=yi+ypas; 
			}
			output.println(); 
			xi=xi+xpas; 
		}
		
		
			
		
		
		
		
		

	}

}
